<?php
/**
 * Created by PhpStorm.
 * User: 睿
 * Date: 2017-12-25
 * Time: 14:02
 */

namespace app\store\controller;


use controller\BasicAdmin;
use PHPExcel;
use PHPExcel_IOFactory;

class Finance extends BasicAdmin
{
    /**
     * 订货
     */
    public function order()
    {
        $this->title = '订货审核';
        $store_id = session('user')['store_id'];
        $get = $this->request->get();

        $db = db('view_store_order_goods')->where('store_id', $store_id)->order('id desc');

        foreach (['name'] as $key) {
            (isset($get[$key]) && $get[$key] !== '') && $db->whereLike('name', "%{$get[$key]}%");
        }

        if (isset($get['date']) && $get['date'] !== '') {
            list($start, $end) = explode('-', str_replace(' ', '', $get['date']));
            $this->assign('date', $start . " - " . $end);

            $start = $start . "  00:00:00";
            $end = $end . " 23:59:59";
            $start = strtotime($start);
            $end = strtotime($end);

            $db->whereBetween('rec_time', ["{$start}", "{$end}"]);

        } else {
            $this->assign('date', '');
        }

        if (isset($get['export']))
            $this->down($db->select());
        else
            return parent::_list($db);
    }

    /**
     * 营业额
     */
    public function sale()
    {
        $this->title = '营业额核对';
        $store_id = session('user')['store_id'];
        $get = $this->request->get();

        $db = db('view_order_details')->where('storeid', $store_id)->order('id desc');

        foreach (['name'] as $key) {
            (isset($get[$key]) && $get[$key] !== '') && $db->whereLike('name', "%{$get[$key]}%");
        }

        if (isset($get['date']) && $get['date'] !== '') {
            list($start, $end) = explode('-', str_replace(' ', '', $get['date']));
            $this->assign('date', $start . " - " . $end);

            $start = $start . "  00:00:00";
            $end = $end . " 23:59:59";
            $start = strtotime($start);
            $end = strtotime($end);

            $db->whereBetween('addtime', ["{$start}", "{$end}"]);

        } else {
            $this->assign('date', '');
        }

        if (isset($get['export']))
            $this->downs($db->select());
        else
            return parent::_list($db);
    }

    /**
     * 导出
     * @param $data
     */
    public function down($data)
    {
        $filename = '订货审核'.date('YmdHis').'.xls';

        $objPHPExcel = new PHPExcel();//新建一个表
        $objSheet = $objPHPExcel->getActiveSheet();//获得当前活动Sheet
        $objSheet->setTitle("订货审核");

        $objSheet
            ->setCellValue('A1', '订货单号')
            ->setCellValue('B1', '商品名称')
            ->setCellValue('C1', '商品属性')
            ->setCellValue('D1', '数量')
            ->setCellValue('E1', '单位')
            ->setCellValue('F1', '收货时间');//填充

        $j = 2;

        foreach ($data as $key => $value) {

            $objSheet
                ->setCellValue('A' . $j, $value['sn'])
                ->setCellValue('B' . $j, $value['name'])
                ->setCellValue('C' . $j, $value['title'])
                ->setCellValue('D' . $j, $value['rec_num'])
                ->setCellValue('E' . $j, $value['unit'])
                ->setCellValue('F' . $j, date('Y-m-d H:i:s', $value['rec_time']));

            $j ++;
        }

        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="' . $filename . '"');
        header('Cache-Control: max-age=0');

        $objWriter->save("php://output");
    }

    /**
     * 导出
     * @param $data
     */
    public function downs($data)
    {
        $filename = '营业额核对'.date('YmdHis').'.xls';

        $objPHPExcel = new PHPExcel();//新建一个表
        $objSheet = $objPHPExcel->getActiveSheet();//获得当前活动Sheet
        $objSheet->setTitle("营业额核对");

        $objSheet
            ->setCellValue('A1', '销售单号')
            ->setCellValue('B1', '商品名称')
            ->setCellValue('C1', '商品属性')
            ->setCellValue('D1', '金额')
            ->setCellValue('E1', '订单时间');//填充

        $j = 2;

        foreach ($data as $key => $value) {

            $objSheet
                ->setCellValue('A' . $j, $value['order_sn'])
                ->setCellValue('B' . $j, $value['name'])
                ->setCellValue('C' . $j, $value['title'])
                ->setCellValue('D' . $j, $value['amount'])
                ->setCellValue('E' . $j, date('Y-m-d H:i:s', $value['addtime']));

            $j ++;
        }

        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="' . $filename . '"');
        header('Cache-Control: max-age=0');

        $objWriter->save("php://output");
    }
}